package com.epro.dx.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.epro.dx.activity.DetailActivity;
import com.epro.dx.activity.LPLocalBrowserActivity;
import com.epro.dx.activity.ProductListActivity;
import com.epro.dx.activity.SpecialActivity;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.model.ModuleChildrenModel;
import com.weipu.common.util.Logger;
import com.weipu.common.util.StringUtil;

/************************
 * @author Tony
 * @Date:2016-4-14下午3:29:14
 * @描述:与活动有关的工具类
 ************************/
public final class BannerUtils {

    private static final String TAG = "ClickBanner";

    /**
     * @创建：Tony 2016-4-14下午3:32:22
     * @描述：活动详情的点击处理方法
     */
    public static void clickActiv(Context mContext, ModuleChildrenModel moduleCModel, int getType, String noticeID) {
        Intent intent = new Intent();
        int activeLinkType = moduleCModel.getLinkType();
        switch (activeLinkType) {
            case ModuleChildrenModel.LINKTYPE_PRODUCTLIST:
//			BannerModel bannerModel = new BannerModel();
//			bannerModel.setBannerID(moduleCModel.getActiveID());
//			bannerModel.setBannerName(moduleCModel.getName());
//			intent.putExtra("picmodel", bannerModel);
                /**
                 * activType = intent.getIntExtra(ModuleChildrenModel.ACTIVTYPE, -1);
                 activID = intent.getStringExtra(ModuleChildrenModel.ACTIVID);
                 String bannerName = intent.getStringExtra("BannerName");
                 * */
                intent.putExtra(ModuleChildrenModel.ACTIVTYPE, getType);
                intent.putExtra(ModuleChildrenModel.ACTIVID, TextUtils.isEmpty(noticeID) ? moduleCModel.getActiveID() : noticeID);
                intent.putExtra("BannerName", moduleCModel.getName());
                intent.setClass(mContext, SpecialActivity.class);
                mContext.startActivity(intent);
                break;
            case ModuleChildrenModel.LINKTYPE_PRODUCTDETAIL:
                int sku = moduleCModel.getSku();
                intent.setClass(mContext, DetailActivity.class);
                Bundle data = new Bundle();

                data.putInt(ServerConstant.Parameter.PARAMETER_NAME_PRODUCT_ID, sku);
                data.putString(ServerConstant.Parameter.PARAMETER_NAME_BACK_ACTION,
                        ServerConstant.ActionConstant.ACTION_GADGET);
                intent.putExtras(data);
                mContext.startActivity(intent);
                break;
            case ModuleChildrenModel.LINKTYPE_OUTURL:
                try {
                    String Url = moduleCModel.getLinkUrl();
                    if (null != Url) {
                        Uri browseUrl = Uri.parse(Url);
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(browseUrl);
                        mContext.startActivity(intent);
                    }
                } catch (ActivityNotFoundException e) {
                    Logger.i(TAG, e.toString());
                }

                break;
            case ModuleChildrenModel.LINKTYPE_INNERURL:
//			BannerModel bModel = new BannerModel();
//			bModel.setBannerID(moduleCModel.getActiveID());
//			bModel.setBannerName(moduleCModel.getName());
//			intent.putExtra("picmodel", bModel);
                intent.putExtra("URL", moduleCModel.getLinkUrl());
                intent.putExtra("PageTitle", moduleCModel.getName());
                intent.setClass(mContext, LPLocalBrowserActivity.class);
                mContext.startActivity(intent);
                break;
            case ModuleChildrenModel.LINKTYPE_CANNOTLINK:
                break;
            case ModuleChildrenModel.LINKTYPE_CLIENT://(NewArravils topSeller mvp latestDiscount四个中一种)
                String toPage = null;
                int activeType = moduleCModel.getActiveType();
                switch (activeType) {
                    case 2:// 2 = TopSeller
                        toPage = Constant.TYPE_TOPSELLERS;
                        break;
                    case 3: // 3 = MVP
                        toPage = Constant.TYPE_MVP24HRS;
                        break;
                    case 4: // 4 = NewArrivals
                        toPage = Constant.TYPE_NEWARRIVALS;
                        break;
                    case 5:// 5 = LatestDiscount
                        toPage = Constant.TYPE_LATESTDISCOUNT;
                        break;

                    default:
                        toPage = Constant.TYPE_WAREHOUSE;
                        break;
                }
//                gotoHomePublicList(mContext, toPage);
                gotoHomePublicList(mContext,moduleCModel, toPage);
                break;

            default:
                break;
        }
    }

    /**
     * @param mContext
     * @param moduleCModel
     * @param type  @创建：Tony 2016-4-14下午3:43:08
     * @描述：首页保留的4个活动入口
     */
    public static void gotoHomePublicList(Context mContext, ModuleChildrenModel moduleCModel, String type) {
        String title = "";
        if (Constant.TYPE_NEWARRIVALS.equals(type)) {
            title = "New Arrivals";
        } else if (Constant.TYPE_MVP24HRS.equals(type)) {
            title = "MVP 24hrs";
        } else if (Constant.TYPE_TOPSELLERS.equals(type)) {
            title = "Top Sellers";
        } else if (Constant.TYPE_LATESTDISCOUNT.equals(type)) {
            title = "Latest Discount";
        } else {
            //US仓版本修改 新增的逻辑，不符合上面四中，则是这种新增情况
            title = moduleCModel.getName();
            if (TextUtils.isEmpty(title)) {
                title = "US Warehouse";
            }
        }

        //保存后缀地址，请求服务器时要用到
        if (moduleCModel != null) {
            String interfaceSuffix = moduleCModel.getInterfaceSuffix();
            StringUtil.saveConfig(Constant.JSON_INTERFACESUFFIX,
                    interfaceSuffix == null || TextUtils.isEmpty(interfaceSuffix) ?
                            Constant.USWAREHOUSE_INTERFACESUFFIX_URL : interfaceSuffix);
        }
        Intent intent = new Intent(mContext, ProductListActivity.class);
        intent.putExtra(Constant.PRODUCTLIST_TYPE, type);
        intent.putExtra(Constant.TO_PRODUCTLIST_FROM, Constant.FROM_HOME);
        intent.putExtra(Constant.PRODUCT_TITLE, title);
        mContext.startActivity(intent);
    }
}
